
package com.cloud.cli.tk.service;

import com.cloud.cli.model.entity.TableEntity;
import com.cloud.cli.tk.mapper.GeneratorMapper;
import com.cloud.cli.tk.util.TkCliGeUtil;
import com.cloud.cli.util.GeneratorUtil;
import org.apache.commons.configuration.Configuration;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.apache.velocity.VelocityContext;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipOutputStream;

/**
 * 代码生成器
 *
 * @author Mr.GUA
 * @email 463540703@qq.com
 * @date 2017年08月25日
 */
@Service
public class GeneratorService {
    @Resource
    private GeneratorMapper generatorMapper;

    public List<Map<String, Object>> queryList(Map<String, Object> map) {
        int offset = Integer.parseInt(map.get("offset").toString());
        int limit = Integer.parseInt(map.get("limit").toString());
        map.put("offset", offset);
        map.put("limit", limit);
        return generatorMapper.queryList(map);
    }

    public int queryTotal(Map<String, Object> map) {
        return generatorMapper.queryTotal(map);
    }

    public Map<String, String> queryTable(String tableName) {
        return generatorMapper.queryTable(tableName);
    }

    public List<Map<String, String>> queryColumns(String tableName) {
        return generatorMapper.queryColumns(tableName);
    }

    public byte[] generatorCode(String[] tableNames) {
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        ZipOutputStream zip = new ZipOutputStream(outputStream);

        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = queryTable(tableName);
            //查询列信息
            List<Map<String, String>> columns = queryColumns(tableName);
            //生成代码
            TkCliGeUtil.generatorCode(table, columns, zip);
        }
        List<String> templates = new ArrayList<>(2);
        templates.add("template/basemapper.java.vm");
        TkCliGeUtil.generatorBaseCode(templates, zip);
        IOUtils.closeQuietly(zip);
        return outputStream.toByteArray();
    }

    public void generatorLocalCode(String[] tableNames) {
        List<Map<String, String>> tablles = new ArrayList<>(tableNames.length);
        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = queryTable(tableName);
            //查询列信息
            List<Map<String, String>> columns = queryColumns(tableName);
            //生成代码
            TableEntity entity = TkCliGeUtil.generatorLocalCode(table, columns);
            if (entity != null) {
                Map<String, String> tbMap = warpTable(entity);
                tablles.add(tbMap);
            }
        }
        TkCliGeUtil.generatorVueRouter(tablles);
    }

    public Map<String, String> warpTable(TableEntity entity) {
        Map<String, String> tbMap = new HashMap<>();
        tbMap.put("secondModuleName", GeneratorUtil.toLowerCaseFirstOne(entity.getClassName()));
        tbMap.put("emptyStr", "");
        tbMap.put("vueClassname", entity.getVueClassname());
        tbMap.put("classPackage", entity.getClassPackage());
        return tbMap;
    }

    public void customCode(String[] tableNames, String[] templateNames) {
        //获取模板列表
        Map<String, String> templateMap = TkCliGeUtil.getTemplates();
        //配置信息
        Configuration config = GeneratorUtil.getConfig();

        for (String tableName : tableNames) {
            //查询表信息
            Map<String, String> table = queryTable(tableName);
            //查询列信息
            List<Map<String, String>> columns = queryColumns(tableName);
            //获取表数据新
            TableEntity tableEntity = TkCliGeUtil.getTableEntity(table, columns, config);
            VelocityContext context = TkCliGeUtil.getVelocityContext(tableEntity, config);
            //获取模板列表
            for (String template : templateNames) {
                //生成代码
                TkCliGeUtil.generateTemplate(templateMap.get(template), context, tableEntity, config);
            }
        }
    }
}
